package com.skyz.wrap.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.skyz.base.dialog.BaseDialogFragment;
import com.skyz.wrap.R;

public class AlertDialogFragment extends BaseDialogFragment {

    public static AlertDialogFragment showDialogFragment(@NonNull FragmentManager manager, String hint, View.OnClickListener listener) {
        AlertDialogFragment loadingDialogFragment = new AlertDialogFragment();
        loadingDialogFragment.config(new Config(hint, listener));
        loadingDialogFragment.show(manager, null);
        return loadingDialogFragment;
    }

    public static AlertDialogFragment showDialogFragment(@NonNull FragmentManager manager, String hint, String btnText, View.OnClickListener listener) {
        AlertDialogFragment loadingDialogFragment = new AlertDialogFragment();
        loadingDialogFragment.config(new Config(hint, btnText, listener));
        loadingDialogFragment.show(manager, null);
        return loadingDialogFragment;
    }

    public static AlertDialogFragment showDialogFragment(@NonNull FragmentManager manager, String hint, String btnText, String canleText,View.OnClickListener listener) {
        AlertDialogFragment loadingDialogFragment = new AlertDialogFragment();
        loadingDialogFragment.config(new Config(hint,canleText, btnText, listener));
        loadingDialogFragment.show(manager, null);
        return loadingDialogFragment;
    }

    Config data;

    private void config(Config data) {
        this.data = data;
    }


    private AlertDialogFragment() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_fragment_alert;
    }

    private TextView tvHint, tvOk, tvCancle;
    private View liner;

    @Override
    protected void onLayoutInflated(View view) {

        tvHint = view.findViewById(R.id.text_hint);
        tvOk = view.findViewById(R.id.tv_ok);
        tvCancle = view.findViewById(R.id.tv_cancel);
        liner = view.findViewById(R.id.liner);
        if (!TextUtils.isEmpty(data.rightText)) {
            tvOk.setVisibility(View.VISIBLE);
            tvOk.setText(data.rightText);
        } else {
            tvOk.setVisibility(View.GONE);
            liner.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(data.leftText)) {
            tvCancle.setVisibility(View.VISIBLE);
            tvCancle.setText(data.leftText);
        } else {
            tvCancle.setVisibility(View.GONE);
            liner.setVisibility(View.GONE);
        }


        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (data != null && data.listener != null) {
                    data.listener.onClick(v);
                }

            }
        });
        tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (data != null && data.listener != null) {
                    data.listener.onClick(v);
                }
            }
        });
        tvHint.setText(data.hint);
        setDialogCancelable(false);
    }

    @Override
    protected WindowManager.LayoutParams initLayoutParams(WindowManager.LayoutParams layoutParams) {
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        return layoutParams;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    public static class Config {
        String hint;
        String leftText;
        String rightText;
        View.OnClickListener listener;

        public Config(String hint, String leftText, String rightText, View.OnClickListener listener) {
            this.hint = hint;
            this.leftText = leftText;
            this.rightText = rightText;
            this.listener = listener;
        }

        public Config(String hint, View.OnClickListener listener) {
            this(hint, "取消", "确定", listener);
        }

        public Config(String hint, String rightText, View.OnClickListener listener) {
            this(hint, "取消", rightText, listener);
        }

        public String getHint() {
            return hint;
        }

        public void setHint(String hint) {
            this.hint = hint;
        }

        public String getLeftText() {
            return leftText;
        }

        public void setLeftText(String leftText) {
            this.leftText = leftText;
        }

        public String getRightText() {
            return rightText;
        }

        public void setRightText(String rightText) {
            this.rightText = rightText;
        }

        public View.OnClickListener getListener() {
            return listener;
        }

        public void setListener(View.OnClickListener listener) {
            this.listener = listener;
        }
    }
}
